package servlet;

import dao.Dao;
import domain.Goods;
import domain.Order;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/addOrderServlet")
public class AddOrderServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String cid = request.getParameter("cid");
        String id = request.getParameter("id");
        String orderid = request.getParameter("orderid");
        String orderdate = request.getParameter("orderdate");
        String address = request.getParameter("address");
        String receivename = request.getParameter("receivename");
        String tele = request.getParameter("tele");
        String paymode = request.getParameter("paymode");
        String snumber = request.getParameter("snumber");
        Dao dao = new Dao();
        Goods goodByCid = dao.findGoodByCid(cid);
        Order order = new Order();
        if (Integer.valueOf(snumber) > goodByCid.getCnumber()) {
            request.setAttribute("orderInfo", "商品数量不足，请重新输入");
            request.getRequestDispatcher("/OrderInfo.jsp?sid=" + cid + "&suserid=" + id).forward(request, response);
        } else {
            float ordertotal = Integer.valueOf(snumber) * (goodByCid.getCprice() * goodByCid.getCdiscounte() / 10);
            if (paymode.equals("网上付款")) {
                order.setOrderid(orderid);
                order.setSid(cid);
                order.setSuserid(Integer.valueOf(id));
                order.setOrderdate(orderdate);
                order.setOrderstate("已付款");
                order.setAddress(address);
                order.setReceivename(receivename);
                order.setTele(tele);
                order.setPaymode(paymode);
                order.setLogisticsstatus("未发货");
                order.setSnumber(Integer.valueOf(snumber));
                order.setOrdertotal(ordertotal);
            } else {
                order.setOrderid(orderid);
                order.setSid(cid);
                order.setSuserid(Integer.valueOf(id));
                order.setOrderdate(orderdate);
                order.setOrderstate("未付款");
                order.setAddress(address);
                order.setReceivename(receivename);
                order.setTele(tele);
                order.setPaymode(paymode);
                order.setLogisticsstatus("未发货");
                order.setSnumber(Integer.valueOf(snumber));
                order.setOrdertotal(ordertotal);
            }
            int i = dao.addOrder(order);
            if (i > 0) {
                response.sendRedirect(request.getContextPath() + "/index.jsp");
            } else {
                request.setAttribute("orderInfo", "提交订单失败，请重试");
                request.getRequestDispatcher("/OrderInfo.jsp?sid=" + order.getSid() + "&suserid=" + order.getSuserid()).forward(request, response);
            }
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
